package com.adams.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.adams.action.view.ViewAction;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.core.service.DataService;
import com.adams.custom.page.core.service.DicTableService;
import com.adams.custom.page.core.service.SQLConstructServie;
import com.adams.custom.page.util.ViewConstant;
import com.adams.custom.page.util.ViewUtil;
import com.adams.utils.DateUtil;

public class OrderAction extends DcxtBaseAction {
    private static final long serialVersionUID = -7261136734687233531L;
    public DicTableService dicTableService;
    private DataService dataService;

    /**
     * 餐桌结账
     * 
     * @return
     * @throws BusinessException
     */
    public String checkOut() throws BusinessException {
        if (ViewUtil.isNuLLStr(viewEventId)) {
            viewEventId = request.getParameter("viewEventId");
        }
        String tableCode = "dcxt_order";
        List<Map> mapList = ViewAction.getDataMap(request);
        for (Map map : mapList) {
            // 获取餐桌号
            String tableID = (String) map.get("dishes_table");
            String sql = "update " + tableCode + " set order_status=";
            StringBuilder sb = new StringBuilder();
            sb.append(sql).append(Contants.ORDER_CHECK_OUT);
            sb.append(" where dishes_table=").append(tableID);
            dicTableService.updateData(tableCode, sb.toString());
        }

        Map returnMap = new HashMap();
        returnMap.put("success", true);
        returnMap.put("msg", "结账成功");
        write(JSONObject.fromObject(returnMap).toString());
        return null;
    }
    
    
    
    public String getViewByEvent() throws Exception
    {
        List<Map> mapList = ViewAction.getDataMap(request);
        Map inputMap = new HashMap();
        if (mapList.size() > 0)
        {
            inputMap = ViewAction.getDataMap(request).get(0);
        }
        
        String status =  (String)inputMap.get("order_status_value");
        if("已结账".equals(status)){
            sendFailureMessage("已经结账，谢谢");
            return null;
        }
        
        
        Map relationParam = getGridrelationparam(request);
        if (null != relationParam)
        {
            inputMap.put(ViewConstant.JSON_GRID_RELATION_PARAM, relationParam);
        }
        Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
                viewEventId, inputMap);
        write(JSONObject.fromObject(map).toString());
        return null;

    }
    

    private static Map getGridrelationparam(HttpServletRequest request)
    {
        Map result = null;
        String paramStr = request
                .getParameter(ViewConstant.JSON_GRID_RELATION_PARAM);
        if (ViewUtil.isNuLLStr(paramStr))
        {
            return result;
        }
        JSONObject data = JSONObject.fromObject(paramStr);
        result = (Map) JSONObject.toBean(data, HashMap.class);
        return result;
    }
    
    public String getDataByViewForm() throws Exception {
        if (ViewUtil.isNuLLStr(formPanelId)) {
            formPanelId = request.getParameter("formPanelId");
        }
        List<Map> mapList = ViewAction.getDataMap(request);
        Map inputMap = new HashMap();
        if (mapList.size() > 0) {
            inputMap = ViewAction.getDataMap(request).get(0);
        }

        // 获取订单ID
        String id = (String) inputMap.get("id");
        // 查询菜单ID与份数
        String tableCode = "dcxt_order_dishes";
        List<String> targetColumncodes = new ArrayList<String>();
        targetColumncodes.add("dishes_id,dishes_price");
        targetColumncodes.add("count");

        LinkedHashMap<String, Map<String, String[]>> conditionColumncodes = new LinkedHashMap<String, Map<String, String[]>>();
        Map<String, String[]> conditionColumncodeMap = new HashMap<String, String[]>();
        String[] menuNameParams = { id };
        conditionColumncodeMap.put(SQLConstructServie.keyWord_equal, menuNameParams);
        conditionColumncodes.put("order_id", conditionColumncodeMap);
        List<Map> result = dataService.getDatas(tableCode, targetColumncodes, conditionColumncodes, null);
       
        float totalPrice =  0;
        String price;
        String count;
        for(Map map : result){
            price =     (String)map.get("dishes_id,dishes_price");       
            count =    (String)map.get("count"); 
            totalPrice +=    Float.parseFloat(price) * Integer.parseInt(count);
        }
        Map map = viewParseService.getDataByViewForm(getUserId(), menuId, formPanelId, inputMap);
        Map dataMap =   (Map) map.get("data");
        dataMap.put("total_money", totalPrice);
        dataMap.put("rate_money", totalPrice);
        dataMap.put("order_status", Contants.ORDER_CHECK_OUT);
        dataMap.put("checkout_time", DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
        write(JSONObject.fromObject(map).toString());
        return null;
    }

    public DicTableService getDicTableService() {
        return dicTableService;
    }

    public void setDicTableService(DicTableService dicTableService) {
        this.dicTableService = dicTableService;
    }

    public DataService getDataService() {
        return dataService;
    }

    public void setDataService(DataService dataService) {
        this.dataService = dataService;
    }

}
